<?php //$Rev:: 3          $ // DO NOT REMOVE

error_reporting(E_ALL);

/**
 * Chat class (Chat is all TOC chat utils.)
 * 
 * @author Andrew Heebner <andrew@evilwalrus.com> 
 * @version $Revision: 3 $
 * @require PHP 5.0
 */
class Chat
{

	public $core;

	/**
	 * __construct: Chat constructor
	 * 
	 * @param object $object Object to store for parent referencing
	 * @access public 
	 * @return void 
	 */
	public function __construct(ATAC $object)
	{
		$this->core = $object;
	} 

	/**
	 * __destruct: Chat destructor
	 * 
	 * @access public 
	 * @return void 
	 */
	public function __destruct()
	{
		unset($this->core);
	} 

	/**
	 * __call: Chat invalid method parser
	 * 
	 * @param string $method Invalid method called
	 * @access private 
	 * @return void 
	 * @throws ATACException
	 */
	public function __call($method, $args)
	{
		throw new ATACException('Invalid method called => (' . __CLASS__ . '::' . $method . ')');
	} 

	/**
	 * aim_chat_invite: Invite a user to join a chat channel
	 * 
	 * @param integer $cid Channel ID to join
	 * @param mixed $nick User to invite to chat (can accept an array, or a single string)
	 * @access public 
	 * @return boolean 
	 * @proto boolean aim_chat_invite(integer chat_id, mixed user)
	 */
	public function aim_chat_invite($cid, $nick)
	{ 
		if (array_key_exists($cid, $this->core->user['chats'])) {
			$invitemsg = 'You have been requested to join %s';
			if (is_array($nick)) {
				foreach ($nick as $n) {
					$n = $this->core->aim_normalize($n);
					$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_invite %d %s %s', $cid, $this->core->aim_encode(sprintf($invitemsg, $this->core->user['chats'][$cid])), $n));
					$this->core->aim_debug(sprintf('%s has been invited to join %s', $n, $this->core->user['chats'][$cid]));
                    return true;
				} 
			} else {
				$nick = $this->core->aim_normalize($nick);
				$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_invite %d %s %s', $cid, $this->core->aim_encode(sprintf($invitemsg, $this->core->user['chats'][$cid])), $nick));
				$this->core->aim_debug(sprintf('%s has been invited to join "%s"', $nick, $this->core->user['chats'][$cid]));
                return true;
			} 
		} 
        return false;
	} 

	/**
	 * aim_chat_join_invite: Join a chat channel (when invited)
	 * 
	 * @param integer $cid Channel ID to join
	 * @access public 
	 * @return void 
	 * @proto void aim_chat_join_invite(integer chat_id)
	 */
	public function aim_chat_join_invite($cid)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_accept %d', $cid));
	}

	/**
	 * aim_chat_create: Create a chat channel
	 * 
	 * @param string $name Channel name to create
     * @param integer $exchange (optional) Channel exchange number (should always be 4)
	 * @access public 
	 * @return void 
	 * @proto void aim_chat_join_invite(string chatname [, integer exchange])
	 */
	public function aim_chat_create($name, $exchange = 4)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_join %d "%s"', $exchange, $name));
	} 

	/**
	 * aim_chat_part: Leave a chat channel
	 * 
	 * @param integer $cid Channel ID to leave
	 * @access public 
	 * @return void 
	 * @proto void aim_chat_part(integer chat_id)
	 */
	public function aim_chat_part($cid)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_leave %d', $cid));
	} 

	/**
	 * aim_chat_warn: Warn a user in a chat channel
	 *
     * @param integer $cid Channel ID
	 * @param string $user User to warn
	 * @param boolean $anon (optional) Warn anonymously
	 * @access public 
	 * @return void 
	 * @proto void aim_chat_warn(integer chat_id, string user [, boolean anonymous_warn])
	 */
	public function aim_chat_warn($cid, $user, $anon = false)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_evil %d %s %s', $cid, $user, (($anon)?'anon':'norm')));
	} 

	/**
	 * aim_chat_send: Send message to channel
	 * 
	 * @param integer $cid Channel ID to send message to
	 * @param string $message Message to send to channel
     * @param string $enc (optional) Encode message via TOC2.0 (UTF-8)
	 * @access public 
	 * @return void 
	 * @proto void aim_chat_send(integer chat_id, string message [, boolean encode])
	 */
	public function aim_chat_send($cid, $message, $enc = false)
	{
        if ($enc === false) {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_send %d %s', $cid, $this->core->aim_encode($message)));
        } else {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_send_enc %d U %s', $cid, utf8_encode($this->core->aim_encode($message))));
        }
	} 

	/**
	 * aim_chat_whisper: Send message to channel user (whisper)
	 * 
	 * @param integer $cid Channel ID to send whisper to
	 * @param string $user User to whisper to
	 * @param string $message Message to send to channel user
     * @param string $enc (optional) Encode message via TOC2.0 (UTF-8)
	 * @access public 
	 * @return void 
	 * @proto void aim_chat_whisper(integer chat_id, string user, string message [, boolean encode])
	 * @deprecated Deprecated since TOC 2.0
	 */
	public function aim_chat_whisper($cid, $user, $message, $enc = false)
	{
        if ($enc === false) {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_whisper %d %s %s', $cid, $this->core->aim_normalize($user), $this->core->aim_encode($message)));
        } else {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_chat_whisper_enc %d U %s %s', $cid, $this->core->aim_normalize($user), utf8_encode($this->core->aim_encode($message))));
        }
	}

} // End Chat

?>